package com.marisfrolg.util;

import android.app.Application;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.device.ScanManager;
import android.os.Handler;
import android.os.Message;


public  class UrovoScanControll implements ReadCodeController{
	private ScanManager mScanManager;
	public Handler m_handler = null;
	public UrovoScanControll(Application app){
		mScanManager = new ScanManager();
		IntentFilter filter = new IntentFilter();
		filter.addAction("urovo.rcv.message");
		app.registerReceiver(mScanReceiver, filter);
	}
	public  void Open() {
		mScanManager.switchOutputMode(-1);
		mScanManager.openScanner();
	}

	public  void Close() {
		mScanManager.closeScanner();
	}

	public  void scan_start() {	
		mScanManager.switchOutputMode(-1);
		mScanManager.startDecode();
	}

	public  void scan_stop() {
		mScanManager.stopDecode();
	}

	@Override
	public void setHandler(Handler handler) {
		m_handler=handler;
	}

	@Override
	public Handler getHandler() {
		return m_handler;
	}
	private BroadcastReceiver mScanReceiver = new BroadcastReceiver() {
		@Override
		public void onReceive(Context context, Intent intent) {

			byte[] barocode = intent.getByteArrayExtra("barocode");
			int barocodelen = intent.getIntExtra("length", 0);
//			byte temp = intent.getByteExtra("barcodeType", (byte) 0);
			String barcodeStr = new String(barocode, 0, barocodelen);
			Message msg = new Message();
			msg.what = ReadCodeController.CODEREAD;
			msg.obj = barcodeStr;
			m_handler.sendMessage(msg);
		}
	};
}
